using Godot;

namespace GoGameClient.script.node.letter;

public partial class OffsetControl : Control
{
    private int _manSpeed = 10;

    private TextureRect _offsetLeft;

    private TextureRect _offsetRight;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _offsetLeft = GetNode<TextureRect>("BackImageLeft");
        _offsetRight = GetNode<TextureRect>("BackImageRight");
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    private bool _flag = true;


    private bool _move;

    public void SetMove(bool move)
    {
        _move = move;
    }


    public override void _Process(double delta)
    {
        _manSpeed = _move ? 10 : 4;
        if (_flag)
        {
            if (_offsetRight.Position > new Vector2(0, 0))
            {
                if (_flag)
                {
                    _flag = false;
                    _offsetLeft.Position = new Vector2(-360 + _offsetRight.Position.X, 0);
                }
            }
        }
        else
        {
            if (_offsetLeft.Position > new Vector2(0, 0))
            {
                if (!_flag)
                {
                    _flag = true;
                    _offsetRight.Position = new Vector2(-360 + _offsetLeft.Position.X, 0);
                }
            }
        }


        _offsetLeft.Position += new Vector2((float)(_manSpeed * delta), 0);
        _offsetRight.Position += new Vector2((float)(_manSpeed * delta), 0);
    }

    public void SetNight(bool night)
    {
        _offsetLeft.Texture =
            GD.Load<Texture2D>($"res://resources/Ui/common/time-{(night ? "night" : "day")}-background.png");
        _offsetRight.Texture =
            GD.Load<Texture2D>($"res://resources/Ui/common/time-{(night ? "night" : "day")}-background.png");
    }
}